﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace hmi
{
    /// <summary>
    /// DebugWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DebugWindow : Window
    {
        private static DebugWindow instance;
        //private bool isUdp = false;
        private DebugWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SetCommunication();
            EventInit();
        }

        public static DebugWindow CreateWindow()
        {
            if (instance == null)
            {
                instance = new DebugWindow();
            }
            return instance;
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            EventInitDestory();
            instance = null;
        }

        private void UdpCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            CloseOptionWindow();
            CommManage.Instance.IsUdp = true;
            SerialCheckBox.IsChecked = !(CommManage.Instance.IsUdp);
        }

        private void SerialCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            CloseOptionWindow();
            CommManage.Instance.IsUdp = false;
            UdpCheckBox.IsChecked = CommManage.Instance.IsUdp;
        }

        private void CloseOptionWindow()
        {
            if (MainWindow.OptionWindow == null)
            {
                return;
            }
            if (MainWindow.OptionWindow.IsVisible)
            {
                MainWindow.OptionWindow.Close();
            }
        }

        private void SetCommunication()
        {
            UdpCheckBox.IsChecked = CommManage.Instance.IsUdp;
            SerialCheckBox.IsChecked = !(CommManage.Instance.IsUdp);
        }

        private void EventInit()
        {
            CommManage.Instance.SendDebugEvent += SendDataTxtAdd;
            CommManage.Instance.ReceiveDebugEvent += ReceiveDataTxtAdd;            
        }

        private void EventInitDestory()
        {
            CommManage.Instance.SendDebugEvent -= SendDataTxtAdd;
            CommManage.Instance.ReceiveDebugEvent -= ReceiveDataTxtAdd;
        }  

        private void SendAreaClearBtn_Click(object sender, RoutedEventArgs e)
        {
            SendAreaTxt.Text = "";
        }

        private void SendAreaBtn_Click(object sender, RoutedEventArgs e)
        {
            string sendAreaData = SendAreaTxt.Text;
            if (sendAreaData == "")
            {
                MessageBox.Show("发送的数据不能为空！", "");
                return;
            }            
            byte[] byteData = Utility.HexStringToBytes(sendAreaData);
            if (byteData != null)
            {
                CommManage.Instance.SendByteData(byteData);
            }                                  
        }

        private void SentDataClearBtn_Click(object sender, RoutedEventArgs e)
        {
            SentDataTxt.Text = "";
        }

        private void ReceivedDataClearBtn_Click(object sender, RoutedEventArgs e)
        {
            ReceivedDataTxt.Text = "";
        }

        private void SendDataTxtAdd(string txt)
        {
            Dispatcher.Invoke(() =>
            {
                SentDataTxt.Text = SentDataTxt.Text + txt + "\n";
            });            
        }

        private void ReceiveDataTxtAdd(string txt)
        {
            Dispatcher.Invoke(() =>
            {
                ReceivedDataTxt.Text = ReceivedDataTxt.Text + txt + "\n";
            });
        }

    }
}
